<?php


namespace app\common\model;


use app\common\lib\ZhyxMongo;

class HealthDiary extends BaseModel
{
    public function getAHealthDiary(string $dataType, array $condition, array $userInfo): array
    {

        $matchList = $condition["match"];
        $result = ZhyxMongo::getInstance()->setCollection("health_diary")
            ->aggregate([
                ['$match' =>
                    ['patientId' => $userInfo["patientId"], 'year' => $condition["year"], 'deleted_at' => 0]],
                ['$unwind' => ['path' => '$' . $dataType]],
                ['$match' => $condition["match"]],
                ['$project' => ['result' => '$' . $dataType]]]);
        $arr = [];
        foreach ($result as $k => $v) {
            $arr[] = (array)$v->result;
        }
        return $arr;
    }

    /**
     * 获取患者最近一次体检
     * @param string $patientId
     */
    public function getLastHealthDiary(string $patientId)
    {
        return ZhyxMongo::getInstance()->setCollection("health_diary")
            ->findOne(
                [
                    "patientId" => $patientId,
                    "year" => (int)date("Y"),
                    "deleted_at" => 0
                ],
                [
                    'projection' => [

                        "bloodSugar" => ['$slice' => -1],
                        "bloodPressure" => ['$slice' => -1],
                        'BMI' => ['$slice' => -1]
                    ]
                ]
            );

    }

    public function getHealthRecord(string $startDate, string $patientId, string $type): array
    {

        $collection = ZhyxMongo::getInstance()->setCollection('health_diary');
        $year = (int)date("Y", strtotime($startDate));
        $arr["result"] = $arr["detail"] = [];
        $lastResult = $collection->aggregate([['$match' => ['patientId' => $patientId, 'year' => $year]], ['$unwind' => ['path' => '$' . $type]], ['$match' => [$type.'.date' => $startDate]], ['$sort' => [$type.'.time' => -1]], ['$limit' => 1], ['$project' => [$type => true, "_id" => false]]]);
        foreach ($lastResult as $k => $v) {
            $arr["result"] = $v;
        }
        if (!empty($arr) && $type==="bloodSugar") {
            $cursor = $collection->aggregate([['$match' => ['year' => $year, "patientId" => $patientId, "deleted_at" => 0]], ['$unwind' => ['path' => '$'.$type]], ['$match' => ['bloodSugar.date' => $startDate]], ['$group' => ['_id' => '$bloodSugar.timeFrame', 'bloodSugar' => ['$last' => '$bloodSugar.bloodSugar'], 'time' => ['$last' => '$bloodSugar.time'], 'kPa' => ['$last' => '$bloodSugar.kPa']]]]);
            foreach ($cursor as $k => $v) {
                $arr["detail"][$v["_id"]] = $v;
            }
        }

        return $arr;
    }

    public function getHealthList(string $startDate, string $endDate, string $patientId): array
    {
        $collection = ZhyxMongo::getInstance()->setCollection('health_diary');
        $year = (int)date("Y", strtotime($startDate));
        $result = $collection->aggregate([['$match' => ['year' => $year, 'patientId' => $patientId, 'deleted_at' => 0]], ['$unwind' => ['path' => '$bloodSugar']], ['$match' => ['$and' => [['bloodSugar.date' => ['$gte' => $startDate]], ['bloodSugar.date' => ['$lte' => $endDate]]]]], ['$group' => ['_id' => ['date' => '$bloodSugar.date', 'type' => '$bloodSugar.timeFrame'], 'sugar' => ['$last' => '$bloodSugar']]], ['$group' => ['_id' => '$_id.date', 'detail' => ['$addToSet' => '$sugar']]], ['$sort' => ['_id' => 1]]]);
        $arr = [];
        foreach ($result as $k => $v) {
            $arr[] = $v;
        }
        return $arr;
    }
}